package example.business;

import com.alibaba.fastjson2.JSON;
import example.config.ApiConfig;
import example.utils.HttpClientUtil;
import example.utils.JinRongResp;
import okhttp3.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class Member {

    public static void register(String mobilephone, String pwd, String regname, JinRongResp expect) {
        // 构建POST请求的表单体
        RequestBody formBody = new FormBody.Builder()
                .add("mobilephone", mobilephone)
                .add("pwd", pwd)
                .add("regname", regname)
                .build();
        try {
            // 使用ApiConfig类中的REGISTER_URL来发送POST请求
            String responseText = HttpClientUtil.post(ApiConfig.REGISTER_URL, formBody);

            // 输出响应内容
            System.out.println("响应体为：" + responseText);

            // 断言响应信息
            assertTrue(responseText.contains("密码长度必须为6~18"));

            // 解析响应体
            JinRongResp body = JSON.parseObject(responseText, JinRongResp.class);
            assertEquals(body.getStatus(), expect.getStatus());
            assertEquals(body.getCode(), expect.getCode());
            assertEquals(body.getMsg(), expect.getMsg());
        } catch (Exception e) {
            System.out.println("异常信息为：" + e.getMessage());
        }
    }
}

